﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Blog.Types.Repository;
using System.Collections.ObjectModel;
using System.Collections;
using System.ComponentModel;

namespace Blog.Core
{
    public class ThemeCollection : BaseObjectCollection<Theme>
    {
        public const int PageSize = 5;

        protected override void LoadExecute()
        {
            foreach (ITheme theme in Factory.Instance.Repository.GetThemes())
                Add(new Theme(theme));
        }

        public Theme GetOne(int id)
        {
            if (id == 0)
                return GenerateNewObject();

            ITheme res = Factory.Instance.Repository.GetTheme(id);
            return res == null ? null : new Theme(res);
        }

        public int Find(int pageId, string text, bool findHeaders, bool findContext)
        {
            Clear();

            pageId --;
            int startRow = pageId < 1 ? 0 : pageId * PageSize; 
            int endRow;

            foreach (ITheme theme in Factory.Instance.Repository.FindThemes(startRow, PageSize, text, findHeaders, findContext, out endRow))
                Add(new Theme(theme));

            return (endRow > PageSize ? Convert.ToInt32(Math.Truncate((double)(endRow / PageSize))) : 0) + 1;
        }

        protected override Theme GenerateNewObject()
        {
            return new Theme();
        }
    }
}
